<?php
	include_once( ROOTPATH .'class'. DS .'star.class.php');
	include_once( ROOTPATH .'class'. DS .'mail.calss.php' );
	
class task{
	function getTaskLists($user_id,$num  = '', $isPage = true , $isMore = false){
		global $dbs,$page,$encrypt ;		
		$split_string = INVITE_SPLIT_STRING ? INVITE_SPLIT_STRING : '|-_-|';
		if( !$user_id )
			$sqlCount = 'SELECT * FROM `'. DB_PRE .'task` ORDER BY `id` DESC';
		else
			$sqlCount = 'SELECT * FROM `'. DB_PRE .'task` WHERE `user_id` = '.$user_id.' ORDER BY `id` DESC';;
		if( !$num )
			$sql = $sqlCount .' limit '. (($page - 1) *  PAGE_SIZE ) .' , '. PAGE_SIZE;
		else
			$sql = $sqlCount .' limit 0 , '. $num ;
		$ListCount	= $dbs -> select($sqlCount);
		$total		= count($ListCount);
		
		$lists = $dbs -> select($sql);
		$html .='<table width="100%" border="0" cellspacing="1" cellpadding="3">'
			  .'<tr class="listTitle">'
				.'<th class="works">'. lang_task.'</th>'
				//.'<th class="name">名称</th>'
				//.'<th class="timeout">截止日期</th>'
				.'<th class="counts">'.works.'</th>'
				//.'<th class="maker">发布者</th>'
				//.'<th class="status">任务状态</th>'
				//.'<th class="hits">点击</th>'
				.'<th class="operate">'.lang_operate.'</th>'
			  .'</tr>';
		foreach($lists as $list){
			$users = new users();			
			$userInfo = $users -> getUser($list['user_id']);
			$status = $list['status'];
			switch($status){
				case '-1' : { $statusShow = '<span class="stop">'.lang_task_forbid.'.</span>' ;  }
				break ;
				
				case '0' : { $statusShow =  '<span class="timeout">'.lang_task_timeout.'.</span>' ;  }
				break ;
				
				case '1' : {
						if(!self::isTimeout($list['timeEnd'],''))
							$statusShow =  '<span class="going">'.lang_is_going.'.</span>' ;
						else
							$statusShow =  '<span class="timeout">'.lang_task_timeout.'.</span>' ;
					
					}
				break ;
			}
			
			$idCode = $encrypt -> code($list['id']) ;
			$lists = $dbs -> select('SELECT * FROM `'.DB_PRE.'task`');
			
			
			$attendCount = $dbs -> select('SELECT count(`id`) as `counts` FROM `'.DB_PRE.'task_attend` WHERE `task_id` = '. $list['id']);
			$attendCount = $attendCount[0]['counts'];
			
			$html .='<tr>'
				.'<td class="thumbs">'
					.'<div class="itemthumb"><a title="'.htmlShow($list['name']).'" href="'.WEB_ROOT_PATH.'?option=task&id='.$encrypt -> code($list['id']).'" target="'.$target.'"><img src="'.self::getTaskThumb($list['thumb']).'" alt="'.htmlShow($list['name']).'" /></a>'
					.'</div>'
					.'<div class="itemdetail">'
						.'<h3 class="itemtitle"><a title="'.htmlShow($list['name']).'" href="'.WEB_ROOT_PATH.'?option=task&id='.$encrypt -> code($list['id']).'" target="'.$target.'">'.htmlShow($list['name']).'</a></h3>'
						.'<p><span>'.lang_timeout.'：'. date('Y-m-d',strtotime($list['timeEnd'])) .'</span><span>'.lang_hits.'：'.$list['hits'].'</span></p>'
						.'<p>'.lang_posted_by.'：<a target="_blank" href="'.urlRewrite('option=home&id='.$list['user_id']).'">'.htmlShow($userInfo['nickName']).'</a></p>'
						.'<p>'.lang_status.'：'.  $statusShow .'</p>'
					.'</div>'
				.'</td>'
				
				//.'<td align=center>'. date('Y-m-d',strtotime($list['timeEnd'])) .'</td>'
				
				.'<td align="center"><a target="_blank" href="./?option=taskAttend&id='.$idCode.'">已投稿( '. $attendCount .' )</a></td>'

				//.'<td align=center><a target="_blank" href="./?option=home&id='. $list['user_id'] .'">'.htmlShow($userInfo['nickName']).'</a></td>'
				//.'<td align=center>'.  $statusShow .'</td>'
				//.'<td align=center>'. $list['hits'] .'</td>'
				.'<td>'
					.'<span class="action rate"><a target="_blank" href="'.WEB_ROOT_PATH.'?option=taskAssess&query='. $encrypt -> code($list['id'] . $split_string . '我是管理员' . $split_string .'我没有时间限制'. $split_string .'9999999999') .'">'.lang_assess_works_assess.'</a></span>';
				
				if( self::isTimeout($list['timeEnd']))	
					$html .='<span class="action taskInvite"><a href="'.WEB_ROOT_PATH.'?option=taskInvite&id='.$idCode.'">'.lang_assess_invite.'</a></span>';
				else
					$html .='<span class="action taskInvite">'.lang_assess_invite.'</span>';;
					
				$html .='<span class="action edit"><a href="'.WEB_ROOT_PATH.'?option=taskAdd&id='.$idCode.'">'.lang_edit.'</a></span>'
					.'<span class="action del"><a href="'.WEB_ROOT_PATH.'?option=taskManageAction&type=action&do=delTask&task_id='. $idCode .'">'.lang_del.'</a></span>'
					
					.'</td>'

				//.'<td align=center><a target="_blank" href="./?option=home&id='. $list['user_id'] .'">'.htmlShow($userInfo['nickName']).'</a></td>'
				//.'<td align=center>'.  $statusShow .'</td>'
				//.'<td align=center>0</td>'
				 

				.'</tr>'
				.'<tr><td colspan="10" class="compart"><hr /></td></tr>';			
		}
		
		if( count($lists) == 0 )
			$html .= '<tr><td colspan="10"><div class="noWorks">未发布任何任务，<a href="'.WEB_ROOT_PATH.'?option=taskAdd">点击这里发布新任务</a>。</div></td></tr>';			
		
		
		$html .= '</table>';
		
		if($isPage)
			$html .= getPages($page,$total,getPageFileUrl(),PAGE_SIZE, PAGE_LEN);
		if($isMore && $total > $num)
			$html .= '<h2 class="more"><a href="'.WEB_ROOT_PATH.'?option=taskList">'. lang_all .' &gt;</a></h2>';
		return $html;
	
	}
	
	function getFileTaskOngoing(&$file_id , $style = 'html'){
		global $dbs ,$encrypt  ;
		$sql = 'SELECT * FROM `'. DB_PRE .'task_attend` WHERE `file_id` = '. $file_id .' ORDER BY `id` DESC';
		$lists = $dbs -> select($sql);
		$html = '<p class="entry">已投稿至:';
		$i = 0;
		$array = array();
		foreach($lists as $list){
			$sql = 'SELECT * FROM `'. DB_PRE .'task` WHERE `id` = '. $list['task_id'] .' limit 0,1';
			$task = $dbs -> select($sql);
			$task = $task[0];
			//if( date('Ymd',strtotime($task['timeEnd'])) < date('Ymd',time())  )
//				continue ;			
			$html .= '<a href="'.urlRewrite('option=task&id='. $encrypt -> code($list['task_id']) ).'" target="_blank">'.$task['name'].'</a>';
			$array[] = $encrypt -> code($list['task_id']);
			$i++ ;
		}
		$html .='</p>';
		if( $i == 0 ) $html = '';
		if($style == 'html'){
			return $html;
		}else{
			return $array ;
		}
	}
	
	
	function getLists($num){
		global $dbs,$encrypt ;		
		$sqlCount = 'SELECT * FROM `'. DB_PRE .'task` ORDER BY `id` DESC';
		$sql = $sqlCount .' limit 0 , '. $num;
		
		$lists = $dbs -> select($sql);
		$html = '';
		$user = new users; 
		foreach($lists as $list){
			$userInfo = $user -> getUser($list['user_id']);
			$html .= '<li>'
			  .'<a href="'.WEB_ROOT_PATH.'?option=task&id='.$encrypt -> code($list['id']) .'" target="_blank"><img class="thumb" src="'.self::getTaskThumb($list['thumb']).'" /></a>'
			  .'<h3><a class="thumbTitle" href="'.WEB_ROOT_PATH.'?option=task&id='.$encrypt -> code($list['id']) .'" target="_blank">'. htmlShow($list['name']).'</a></h3>'
			  .'<h3>'.lang_posted_by.': <a href="'.urlRewrite('option=home&id='.$list['user_id']) .'" target="_blank">'. htmlShow($userInfo['nickName']) .'</a></h3>'
			  .'<h3>'.lang_submit.': <b>'. self::getTaskAttendCounts($list['id']) .'</b> 件</h3>'
			  .'<h1>'. getTag($list['id'],'_blank') .'</h1>'
			.'</li>';
		}
		$html .= '<div class="clear"></div>';
		return $html;
		
	
	}
	
	
	
	function getTaskAttendCounts($task_id){
		global $dbs,$encrypt ;		
		$sql = 'SELECT count(*) as `counts` FROM `'. DB_PRE .'task_attend` WHERE `task_id` = '.$task_id ;
		$counts = $dbs -> select($sql);
		$counts = $counts[0]['counts'];
		return $counts;
	}
	
	function getAttendLists($task_id ,$num  = '', $isPage = true , $isMore = false){
		global $dbs,$page,$encrypt;
		$sqlCount = 'SELECT * FROM `'. DB_PRE .'task_attend` WHERE `task_id` = '.$task_id.' ORDER BY `id` DESC';
		if( !$num )
			$sql = $sqlCount .' limit '. (($page - 1) *  PAGE_SIZE ) .' , '. PAGE_SIZE;
		else
			$sql = $sqlCount .' limit 0 , '. $num;		
		$ListCount	= $dbs -> select($sqlCount);
		$total		= count($ListCount);
		
		$lists = $dbs -> select($sql);
		
		
		$html .='<table width="100%" border="0" cellspacing="1" cellpadding="3">'
			  .'<tr class="listTitle">'
				.'<th class="works">'.lang_works.'</td>'
				//.'<th class="name">名称</td>'
				.'<th class="uploadUser">'.lang_posted_by.'</td>'
				.'<th class="attendTime">'.lang_time.'</td>'
				.'<th class="maker">'.lang_grade.'</td>'
				.'<th class="operate">'.lang_operate.'</td>'
			  .'</tr>';
			  
		$user = new users();	  
		foreach($lists as $list){
			
			$files = $dbs -> select('SELECT * FROM `'. DB_PRE .'file` WHERE `id` ='. $list['file_id'] .' limit 0,1' );
			$files = $files[0];	
			$user_id = 	$files['user_id'];
			$userInfo = $user -> getUser($user_id);
			
			$sql = 'SELECT SUM(`star`) as `star` FROM `'. DB_PRE .'star_assess` WHERE `task_id` = '. $list['task_id'] .' and `file_id` = '. $list['file_id'] ;
			$stars = $dbs -> select($sql);
			$stars = $stars[0];
		
			//$task = $dbs -> select('SELECT * FROM `'.DB_PRE.'task` WHERE `id` = '.$list['task_id'] );
			//$task = $task[0];	
			
			$task_id = $encrypt -> code($list['task_id']) ;
			
			$file = $dbs -> select('SELECT * FROM `'.DB_PRE.'file` WHERE `id` = '.$list['file_id'] );
			$file = $file[0];
			$file_id = $encrypt -> code($list['file_id']);
			$html .='<tr>'
				.'<td class="thumbs"><div class="itemthumb"><a class="beautyImg" target="_blank" title="'.htmlShow($file['name']).'" href="'.urlRewrite('option=media&id='.$file_id).'"><img src="'.self::getTaskThumb($file['thumb']).'" alt="'.htmlShow($file['name']).'" class="thumb" /></a></div>'
					.'<div class="itemdetail"><h3 class="itemtitle">'
					.'<a target="_blank" title="'.htmlShow($file['name']).'" href="'.urlRewrite('option=media&id='.$file_id).'">'. htmlShow($file['name']).'</a>'
					.'</h3>
					标识码:<span class="itemid">'. $file_id .'</span>'
					
					.'</div></td>'
				.'<td align=center><a href="'.urlRewrite('option=home&id='.$userInfo['id']).'" target="_blank">'. htmlShow($userInfo['nickName']).'</a></td>'
				.'<td align=center>'. date('Y-m-d',strtotime($list['created'])) .'</td>
				<td align=center><font class="star yellow">';
				
				if(  self::taskCreaterView($user_id) ){
				$html .='<a href="'.WEB_ROOT_PATH.'?option=taskAttendShow&id='.$file_id.'" class="a"></a> x '.(int)$stars['star'].'';
				}else{
					$html .='<i class="a"></i> x '.(int)$stars['star'].'';
				}
				
				$html .='</font></td><td>
					<span class="action view"><a target="_blank" href="'.urlRewrite('option=media&id='.$file_id).'">'.lang_view.'</a></span>';					
				if(  self::taskCreaterView($user_id) ){
					$html .='<span class="action del"><a href="'.WEB_ROOT_PATH.'?option=taskCancel&task_id='.$encrypt -> code($list['task_id']).'&file_id='.$file_id.'">退回投稿</a></span>
					<span class="action edit addHonor"><a href="'.WEB_ROOT_PATH.'?option=honorAdd&task_id='. $task_id .'&file_id='.$file_id.'">'.lang_add_honor.'</a></span>';
				}
				$html .='</td>
				</tr>
				<tr><td colspan="10" class="compart"><hr /></td></tr>';			
		}
		$html .= '</table>';
		
		if($isPage)
			$html .= getPages($page,$total,getPageFileUrl('id='.$task_id,false),PAGE_SIZE, PAGE_LEN,false);
		if($isMore && $total > $num)
			$html .= '<h2 class="more"><a href="'.urlRewrite('option=taskList') .'">'. lang_all .' &gt;</a></h2>';
		return $html;
	
	}
	
	function taskCreaterView(&$user_id){
		global $user;
		if(  ( $user -> powder() >= 5 && $user_id == $user -> id()   )  ||   $user -> powder()  >= 9 ){
			return true ;
		}
		return false;	
	}
	
	function getTaskFileScoreList($file_id , $num  = '', $isPage = true , $isMore = false){
		
		global $dbs,$page,$encrypt;
		$sqlCount = 'SELECT * FROM `'. DB_PRE .'star_assess` WHERE `file_id` = '.$file_id.' ORDER BY `id` DESC';
		if( !$num )
			$sql = $sqlCount .' limit '. (($page - 1) *  PAGE_SIZE ) .' , '. PAGE_SIZE;
		else
			$sql = $sqlCount .' limit 0 , '. $num;		
		$ListCount	= $dbs -> select($sqlCount);
		$total		= count($ListCount);
		
		$lists = $dbs -> select($sql);
		
		$stars = new star();
		
		
		$sql = 'SELECT SUM(`star`) as `star` FROM `'. DB_PRE .'star_assess` WHERE `file_id` = '.$file_id ;
		$starCount = $dbs -> select($sql);
		$starCount = $starCount[0]['star'];
		
		$html .='<h3 class="highBig">总得分<span class="star yellow"><i class="a"></i> x '. (int)$starCount .' </span></h3>';
		$html .='<table width="100%" border="0" cellspacing="0" cellpadding="0">'
			  .'<tr class="listTitle">'
				.'<td class="invite">评阅人</td>'
				.'<td class="status">评阅状态</td>'
				.'<td class="score">得分</td>'
			  .'</tr>';
		foreach($lists as $list){
			
			$invite = '<span title="受邀评阅人">'.$list['invite'].'</span>';
			if( 0 != (int)$list['user_id'] ){
				$users = new users();			
				$userInfo = $users -> getUser($list['user_id']);
				$userName = $userInfo['userName'];
				$invite = '<a target="_blank" title="'.htmlShow($userInfo['userName']).'" href="'.urlRewrite('option=home&id='.$userInfo['id']).'">'. $userName.'</a>';
			}
			
			$timeOut = self::isTimeout($list['timeEnd'],'') ? lang_task_timeout :'<a>未过期</a>' ;
			$html .='<tr>'
				.'<td>'.$invite.'</td>'
				.'<td align=center>'. $timeOut .'</td>'
				.'<td align=center><span class="star yellow">'. $stars -> creatScore($list['star']) .'</span></td>';			
		}
		$html .= '</table>';
		
		if($isPage)
			$html .= getPages($page,$total,getPageFileUrl(),PAGE_SIZE, PAGE_LEN);
		if($isMore && $total > $num)
			$html .= '<h2 class="more"><a href="'.urlRewrite('option=taskList') .'">'. lang_all .' &gt;</a></h2>';
		return $html;
	
	}
	
	function getInviteList( &$id ){
		global $dbs, $user , $encrypt ,$page ;		
		
		$task_id = $encrypt -> recode($id);
		
		$sqlCount = 'SELECT * FROM `'. DB_PRE .'task_invite` WHERE `task_id` = '. $task_id .' ORDER BY `isSend`,`timeEnd` DESC';

		$sql = $sqlCount .' limit '. (($page - 1) *  (PAGE_SIZE * 10) ) .' , '. (PAGE_SIZE * 10);
		$ListCount	= $dbs -> select($sqlCount);
		$total		= count($ListCount);		
		$lists 		= $dbs -> select($sql);
		$html 		= '';
		$i 			= ($page - 1) *  (PAGE_SIZE * 2);
		foreach($lists as $list){
			$i++;
			$status = $list['isSend'] == "1" ? '已经发送邀请邮件' : '<b class="high">邮件发送失败</b> <a href="#">重发邮件</a>';
			$html .= '<ul>
				<li class="invite" title="'.$list['invite'].'"><span>受邀:</span>'. subString($list['invite'],20) .'</li>
				<li><span>截止:</span>'. $list['timeEnd']  .'</li>
				<li><span>范围:</span>前 '. $list['selectionArea'] .' 名</li>
				<li><span>状态:</span>'. $status.'</li>
				<li class="order">'.$i.'</li>
			</ul>';
		}
		if( count( $lists) == 0 )
			$html .= '<p class="noWorks">还没有发送过邀请</p>';
		$html .= '<div class="clear"></div>';
		$html .= getPages($page,$total,getPageFileUrl().'&id='.$id ,PAGE_SIZE * 10, PAGE_LEN);
		return $html;
		
	
	}
	
	
	
	
	function checkOwner($user_id){
		global $user;
		$id = $user -> id();
		if( $user_id != $id &&  $user -> powder() < 9  ){
			header('Location:./');
			exit('<font class="error">这个活动不是您创建的.</font>');
		}	
	}
	
	
	
	function getTaskThumb($img){
		if(!$img)
			return TASK_THUMB;
		return WEB_ROOT_PATH . $img;
	}
	function isTimeout($time,$id=''){
		global $dbs;
		$str = array('-',' ',':');
		if(''!=$id){
			$sql = 'SELECT * FROM `'. DB_PRE.'task` WHERE `id` ='. $id;
			$lists = $dbs -> select($sql);
			$lists = $lists[0];
			$timeEnd = str_replace($str,"",$lists['timeEnd']);
			$timeEnd = floor( $timeEnd );
		}else{
			$timeEnd = str_replace($str,"",$time);
			$timeEnd = floor( $timeEnd );
		}
		// 2010-06-25 00:00:00
		// 2010-06-26 00:00:00                       
		if( floor(date('Ymd',time()))  > $timeEnd ){
			return true ;
		}else{
			return false;
		}		
	}
	
	function del( &$task_id ){
		global $dbs,$user;
		
		// 检查是否存在
		$sql = 'SELECT * FROM `'.DB_PRE.'task` WHERE `id` = '. $task_id ;
		$lists = $dbs -> select($sql);
		$list  = $lists[0];
		if(count($list) == 0 )
			exit( infoPage('这个任务不存在') );
			
		
		// 检查是否属于本人/管理员
		if($list['user_id'] != $user -> id() && $user -> powder() < 9)
			exit(infoPage('失败，这不是您创建的任务'));
	
		
		// 删除任务
		$dbs -> delete( DB_PRE .'task' , '`id` = '. $task_id );
		
		// 删除任务投稿作品
		$dbs -> delete( DB_PRE .'task_attend' , '`task_id` = '. $task_id );
		
		// 删除任务邀请
		$dbs -> delete( DB_PRE .'task_invite' , '`task_id` = '. $task_id );
		
		
	//	// 检测有没有参加过别的任务。
	//	$sql = 'SELECT * FROM `'.DB_PRE.'task_attend` WHERE  `task_id` != '. $task_id .' and `file_id` = '. $array['file_id']  ;
	//	$lists = $dbs -> select($sql);
	//	
	//	// 说明参加过 , 什么都不做
	//	if(count($lists) > 0 )
	//		;
	//	// 说明只有这次 
	//	else{
	//		// 锁定作品
	//		$update['status'] = '1';
	//		$dbs -> update( DB_PRE .'file', $update , ' `id` = '. $array['file_id'] );
	//	}
		
		
		exit( infoPage('<div class="ok">成功删除。</div>'));
	
	}
	
	
	
	
	function taskAttend($task_id,$file_id , $isSilent = false ){
		global $dbs,$user,$dataType;
		$array['task_id'] 			= $task_id ;
		$array['file_id'] 			= $file_id ;
		$array['created']			= date('Y-m-d H:i:s',time());
		$array['ip'] 				= ip();
		
		// 检查是否存在
		$sql = 'SELECT * FROM `'.DB_PRE.'file` WHERE `id` = '. $array['file_id'] ;
		$lists = $dbs -> select($sql);
		if(count($lists) == 0 ){
			if( !$isSilent)
				exit('<div class="error">这个作品不存在。</div>');
			else
				return ;
		}
		
		
		// 检查任务是否过期
		$sql = 'SELECT * FROM `'.DB_PRE.'task` WHERE  `id` = '. $array['task_id']  .' AND `timeEnd` < "'. date('Y-m-d',time()) .'" limit 0,1' ;
		$lists = $dbs -> select($sql);
		if(count($lists) > 0  ){
			if( $dataType == 'ajax' )
				exit('{"status":"error","msg":"任务已经过期,系统关闭."}');
			else
				exit('<div class="error">任务已经过期,系统关闭.</div>');
		}
		
		
		// 检查是否已经报名
		$sql = 'SELECT * FROM `'.DB_PRE.'task_attend` WHERE  `task_id` = '. $array['task_id'] .' and `file_id` = '. $array['file_id']  ;
		$lists = $dbs -> select($sql);
		if(count($lists) > 0  ){
			if( !$isSilent){
				if( $dataType == 'ajax' )
					exit('{"status":"error","msg":"这个作品已经报名了"}');
				else
					exit('<div class="error">这个作品已经报名了。</div>');
			}
				
			else
				return ;
		}
			
			
		
		// 检查是否属于本人
		$sql = 'SELECT * FROM `'.DB_PRE.'file` WHERE `id` = '. $array['file_id'] ;
		$lists = $dbs -> select($sql);
		$lists = $lists[0];
		if($lists['user_id'] != $user -> id() && $user -> powder() < 9){
			if( $dataType == 'ajax' )
				exit('{"status":"error","msg":"提交失败，因为你不是这个作品的上传人。"}');
			else
				exit('<div class="error">提交失败，因为你不是这个作品的上传人。</div>');
		}
			
		
		
		
		
		// 锁定作品
		$update['status'] = '-1';
		//$update['lock'] = '1';
		$dbs -> update( DB_PRE .'file', $update , ' `id` = '. $array['file_id'] );
		
		
		$dbs -> insert( DB_PRE.'task_attend' , $array);
		if(!$isSilent)
			exit('<div class="ok">已经成功报名。</div>');
		else
			return ;
	
	}
	
	
	
	
	
	function taskCancel($task_id,$file_id,$message=''){
		global $dbs,$user, $encrypt;
		$array['task_id'] 			= $task_id ;
		$array['file_id'] 			= $file_id ;
		$array['created']			= date('Y-m-d H:i:s',time());
		$array['ip'] 				= ip();
		
		
			
		
		// 检查任务是否操作者
		$sql = 'SELECT * FROM `'.DB_PRE.'task` WHERE `id` = '. $task_id ;
		$list = $dbs -> select($sql);
		$list = $list[0];
		if( $user -> powder() < 9 ){
			if($list['user_id'] != $user -> id()  )
				exit('<li class="error">操作失败, 您没有权限.</li>');
	
		}
		
		
		
		// 检测有没有参加过别的任务。
		$sql = 'SELECT * FROM `'.DB_PRE.'task_attend` WHERE `file_id` = '. $array['file_id'] .' AND `task_id` != '. $array['task_id'] ;
		$lists = $dbs -> select($sql);		
		// 说明参加过别的活动
		if(count($lists) > 0 ){
			;			
		}else{
			// 解锁作品
			//$update['status'] = '1';
			//$dbs -> update( DB_PRE .'file', $update , ' `id` = '. $array['file_id'] );
		}
		//  从本次任务中删除
		
			
		// 检查是否存在
		$sql = 'SELECT * FROM `'.DB_PRE.'file` WHERE `id` = '. $array['file_id']  .' limit 1';
		$file = $dbs -> select($sql);
		$file = $file[0];
		/*if(count($lists) == 0 )
			exit('<li class="error">这个作品不存在</li>');*/
		$userInfo = $dbs -> select('SELECT `id`,`userName`,`nickName` FROM `'.DB_PRE.'user` WHERE `id` ='. (int)$file['user_id'] );
		$userInfo = $userInfo[0];
		
		$dbs -> delete( DB_PRE.'task_attend' , '`task_id` = '. $array['task_id'] .' and `file_id` = '. (int)$array['file_id'] );
		if( (int)$userInfo['id'] > 0 ){
			$mail = new email ;		
			$sengMail = $mail -> send( $to= $userInfo['userName'], $name= $userInfo['nickName'],$title = '您的参赛作品被退回',$body  ='
				您好，您的作品: [<a href="'.urlRewrite('option=media&id='.$encrypt -> code($file_id)).'" target="_blank">'. toText($file['name']).'</a>] 不符合参赛要求，被退回.
				<hr />
				<h2>管理员留言:</h2>
				<div>'. htmlShow($message)  .'</div>
				
			
			');
		}
		//$dbs -> delete( DB_PRE.'task_attend' , '`task_id` = '. $array['task_id'] .' and `file_id` = '. $array['file_id'] );
		exit('{"status":"ok","msg":"已经退回作品"}');
	
	}
	
	function allowTaskCreator( $file_id = 0 ){
		global $dbs;
		$file_id  = (int)$file_id ;
		$task_ids = $dbs -> select('SELECT distinct `task_id` FROM `'.DB_PRE.'task_attend` WHERE `file_id` = '. $file_id );
		$allowTaskCreator = array();
		foreach( $task_ids as $tmp ){
			$taskTmp = $dbs -> select('SELECT * FROM `'.DB_PRE.'task` WHERE `id` = '. $tmp['task_id'] .' limit 0,1');
			$allowTaskCreator[] = $taskTmp[0]['user_id'];
		}
		return $allowTaskCreator;
	}
		
		
}


class taskAssess{
	function getList( $task_id ,$invite , $timeEnd , $num  = 1 , $orderBy = 'id' ,$orderByType = 'DESC', $isPage = true , $isMore = false ){
		global $dbs,$user, $page,$encrypt,$queryCode,$queryCode;
		$sqlCount = 'SELECT * FROM `'. DB_PRE .'task_attend` WHERE `task_id` = '.$task_id.' ORDER BY `'.$orderBy.'` '. $orderByType ;
		
		if(   ($page - 1) *  PAGE_SIZE  +   PAGE_SIZE  > $num )
			$sql = $sqlCount .' limit '. (  $num -  PAGE_SIZE < 0 ? 0 : $num -  PAGE_SIZE ) .' , '. $num ;
		else
			$sql = $sqlCount .' limit '. (($page - 1) *  (PAGE_SIZE * 10) ) .' , '.  (PAGE_SIZE * 10);
 
		$ListCount	= $dbs -> select($sqlCount);
		$total		= count($ListCount);
		
		$lists = $dbs -> select($sql);
		$html .='<ul class="taskAssessList">';
		foreach($lists as $list){
			$file = $dbs -> select('SELECT * FROM `'.DB_PRE.'file` WHERE `id` = '.$list['file_id'] );
			$file = $file[0];
			$file_id = $encrypt->code($list['file_id']) ;
			$html .='<li>
				<a target="_blank" href="'.WEB_ROOT_PATH.'?option=media&id='.$file_id.'&showType=assess&query='.$queryCode.'"><img class="thumb" src="'.self::getTaskThumb($file['thumb']).'" alt="'.htmlShow($file['name']).'" /></a>
				<h1>'.htmlShow($file['name']).'</h1>
				<a class="view" target="_blank" title="'.htmlShow($file['name']).'" href="'.WEB_ROOT_PATH.'?option=media&id='.$file_id.'&showType=assess&query='.$query.'">'.lang_view.'&gt;&gt;</a>';				
				
				if( $invite == '我是管理员' )
					$sql = 'SELECT * FROM `'.DB_PRE.'star_assess` WHERE `task_id` = '.$task_id .' and `file_id` = '.$list['file_id'].' and `user_id` = '.$user -> id();
				else
					$sql = 'SELECT * FROM `'.DB_PRE.'star_assess` WHERE `task_id` = '.$task_id .' and `file_id` = '.$list['file_id'].' and `invite` = "'.$invite.'" and `timeEnd` = "'. $timeEnd .'" ';
				
				//echo $sql .'<br />';
				$stars = $dbs -> select($sql);
				$stars = $stars[0];
				//print_r($counts);
				if( count($stars) == 0 ){				
					$html .='<div class="star yellow">'.lang_grade.' : <span>';
					
				}else{
					$html .='<div class="star yellow">'.lang_grade.' : <span>';
				}
				for($i=1; $i<= TASK_STAR ; $i++){
					$class = $i <= (int)$stars['star'] ? ' class="a" ' : '';
					$html .='<a'.$class.' href="'.WEB_ROOT_PATH.'?option=taskAssessAction&type=action&do=star&star='.$i.'&id='.$file_id.'&query='.$queryCode.'"></a>';
				}
				
				
				$html .='</span></div>';
			$html .='</li>';                			
		}
		$html .= '<div class="clear"></div></ul>';
		
		if($isPage)
			$html .= getPages($page,$total,getPageFileUrl('query='.$queryCode .'&orderByType='. $orderByType , false) ,  (PAGE_SIZE * 10) , PAGE_LEN,$pageRewrite=false);
		if($isMore && $total > $num)
			$html .= '<h2 class="more"><a href="'.WEB_ROOT_PATH.'option=taskList">'.lang_all.' &gt;</a></h2>';
		return $html;
		
	}
	
	function getTaskThumb($img){
		if(!$img)
			return TASK_THUMB;
		return $img;
	}
}
?>